//
// Created by Alex on 2021/12/1.
//

#include "GeoJsonReader.h"
#include "GeoPoint.h"
#include "LonLat.h"
#include "GeoPolyLine.h"
#include "GeoLayer.h"
#include "GeoPoint.h"
#include "GeoPolyLine.h"
#include "GeoPolygon.h"
#include <QJsonDocument>
#include <QVariant>
#include <QFile>
#include <QByteArray>
#include <QJsonParseError>
#include <QJsonObject>
#include <QJsonArray>
#include <iostream>
#include <QDebug>
#define mout qDebug() <<"["<<__FILE__<<":"<<__LINE__<<"]"
using namespace  std;
GeoJsonReader::GeoJsonReader() {

}

GeoJsonReader::GeoJsonReader(QString path) {
    this->path = path;
}



bool GeoJsonReader::read(GeoLayer *layer) {
    //打开json文件
    QFile jsonFile(this->path);


    jsonFile.open(QIODevice::ReadOnly);
    QByteArray allData = jsonFile.readAll();
    jsonFile.close();

    //读取文件
    QJsonParseError json_error;
    QJsonDocument jsonDoc(QJsonDocument::fromJson(allData,&json_error));
    if(json_error.error != QJsonParseError::NoError){
        cout << "json error!";
        return false;
    }
    //创建对象
    QJsonObject rootObj = jsonDoc.object();
    //读取属性
    if(rootObj.contains("properties")){
        QJsonObject properties = rootObj.take("properties").toObject();
        QMap<QString,QVariant> propertiesmap = properties.toVariantMap();

        for(int i = 0;i<propertiesmap.size();++i){
            QStringList keys = properties.keys();
            layer->addProperties(keys.at(i),propertiesmap.value(keys.at(i)));
        }
    }
    QJsonArray features = rootObj.take("features").toArray();
    for(int i=0;i<features.size();++i){
        QJsonObject feature = features[i].toObject();
        QJsonObject geometry = feature.take("geometry").toObject();
        GeoObject *obj = nullptr;
        //读取点数据
        if(geometry.value("type")=="Point"){
            obj = new GeoPoint(LonLat(geometry.value("coordinates").toArray()[0].toDouble(),geometry.value("coordinate").toArray()[1].toDouble()));
            obj->addProperties("type","Point");
        }
        //读取线数据
        else if(geometry.value("type") == "LineString"){
            obj = new GeoPolyLine();
            QJsonArray coordinates = geometry.take("coordinates").toArray();
            for(int i=0;i<coordinates.size();++i){
                obj->addPoint(LonLat(coordinates.at(i).toArray()[0].toDouble(),coordinates.at(i).toArray()[1].toDouble()));
            }
            obj->addProperties("type","LineString");
        }
        //读取面数据
        else if(geometry.value("type") == "Polygon"){
            obj = new GeoPolygon();
            QJsonArray pts = geometry.take("coordinates").toArray();
            for(int i=0;i<pts.size();++i){
                obj->addPoint(LonLat(pts.at(i).toArray()[0].toDouble(),pts.at(i).toArray()[1].toDouble()));
            }
            obj->addProperties("type","Polygon");
        }
        //其他数据暂不处理
        else{}

        //读取属性
        if(feature.contains("properties")){
            QJsonObject properties = feature.take("properties").toObject();
            QMap<QString,QVariant> propertiesmap = properties.toVariantMap();

            for(int i = 0;i<propertiesmap.size();++i){
                QStringList keys = properties.keys();
                layer->addProperties(keys.at(i),propertiesmap.value(keys.at(i)));
            }
        }
        if(obj != nullptr)
            layer->addObject(obj);
    }
    return true;
}











